package com.beyond.trace.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beyond.trace.domain.TraceProductCertificate;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.beyond.trace.mapper.TraceProductQualityCheckMapper;
import com.beyond.trace.domain.TraceProductQualityCheck;
import com.beyond.trace.service.ITraceProductQualityCheckService;

/**
 * 产品质量检测信息Service业务层处理
 * 
 * @author yao
 * @date 2022-03-02
 */
@Service
public class TraceProductQualityCheckServiceImpl extends ServiceImpl<TraceProductQualityCheckMapper, TraceProductQualityCheck> implements ITraceProductQualityCheckService
{
    @Resource
    private TraceProductQualityCheckMapper traceProductQualityCheckMapper;

    /**
     * 查询产品质量检测信息
     * 
     * @param checkId 产品质量检测信息ID
     * @return 产品质量检测信息
     */
    @Override
    public TraceProductQualityCheck selectTraceProductQualityCheckById(Long checkId)
    {
        QueryWrapper<TraceProductQualityCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_id",checkId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceProductQualityCheckMapper.selectOne(queryWrapper);
    }

    /**
     * 查询产品质量检测信息列表
     * 
     * @param traceProductQualityCheck 产品质量检测信息
     * @return 产品质量检测信息
     */
    @Override
    public List<TraceProductQualityCheck> selectTraceProductQualityCheckList(TraceProductQualityCheck traceProductQualityCheck)
    {
        try{  //解决未传token的问题
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                traceProductQualityCheck.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return traceProductQualityCheckMapper.selectTraceProductQualityCheckList(traceProductQualityCheck);
    }

    /**
     * 新增产品质量检测信息
     * 
     * @param traceProductQualityCheck 产品质量检测信息
     * @return 结果
     */
    @Override
    public boolean insertTraceProductQualityCheck(TraceProductQualityCheck traceProductQualityCheck)
    {
        traceProductQualityCheck.setCreateTime(DateUtils.getNowDate());
        traceProductQualityCheck.setUpdateTime(DateUtils.getNowDate());
        traceProductQualityCheck.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        traceProductQualityCheck.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        traceProductQualityCheck.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        //return traceProductQualityCheckMapper.insertTraceProductQualityCheck(traceProductQualityCheck);
        return super.save(traceProductQualityCheck);
    }

    /**
     * 修改产品质量检测信息
     * 
     * @param traceProductQualityCheck 产品质量检测信息
     * @return 结果
     */
    @Override
    public int updateTraceProductQualityCheck(TraceProductQualityCheck traceProductQualityCheck)
    {
        traceProductQualityCheck.setUpdateTime(DateUtils.getNowDate());
        traceProductQualityCheck.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return traceProductQualityCheckMapper.updateById(traceProductQualityCheck);
    }

    /**
     * 批量删除产品质量检测信息
     * 
     * @param checkIds 需要删除的产品质量检测信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceProductQualityCheckByIds(Long[] checkIds)
    {
        QueryWrapper<TraceProductQualityCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("check_id", Arrays.asList(checkIds));
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceProductQualityCheckMapper.delete(queryWrapper);
    }

    /**
     * 删除产品质量检测信息信息
     * 
     * @param checkId 产品质量检测信息ID
     * @return 结果
     */
    @Override
    public int deleteTraceProductQualityCheckById(Long checkId)
    {
        QueryWrapper<TraceProductQualityCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_id", checkId);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return traceProductQualityCheckMapper.delete(queryWrapper);
    }
}
